$(function () {
  // console.log(111)
  let layer = layui.layer;

  let mobile = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;

  // let pwd = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/
  $('.btn').on('click', function () {
    if (!mobile.test($('#number').val().trim())) {
      return layer.msg('手机格式不正确');
    }
    // 发送 POST 请求
    axios({
      method: 'post',
      url: 'http://124.223.14.236:8060/home//index/login',
      data: {
        mobile: $('#number').val(),
        password: $('#password').val(),
      },
    }).then(function (res) {
      console.log(res);
      // console.log(res.data.data.token)

      if (res.data.errno == 1000) {
        return layer.msg('密码不正确');
      }
      layer.msg('登录成功');
      token = res.data.data.token;
      localStorage.setItem('token', token);
      location.href = './index.html';
    });
  });
  homePage();
  // 首页和全局数据
  function homePage() {
    axios({
      method: 'GET',
      url: 'http://124.223.14.236:8060/home/index/index',
    }).then(function (res) {
      // console.log(res);
      // 导航栏
      let strNav = res.data.data.allCate
        .map(item => {
          return `
          <li class="menu-item menu-item-type-taxonomy menu-item-object-category">
              <a href="list.html" id=${item.id}><span class="fa fa-bullhorn iconfont ${item.icon}"></span>${item.catename}</a>
          </li>
          `;
        })
        .join('');
      $('#nav').append(strNav);
    });
  }
  userCenter();
  // 导航栏跳转
  function getNavSkip() {
    $('#nav').on('click', 'a', function (e) {
      e.preventDefault();
      window.open('list.html?id=' + $(this).attr('id'));
    });
  }
  getNavSkip();
});
